package org.grayrabbit.cms.publish.generate;

import java.util.ArrayList;
import java.util.List;

import org.grayrabbit.cms.publish.api.Publishable;



public class CatMission extends Mission{
	private int currentCatIndex;
	private int currentCatListIndex;
	private int currentContentIndex;
	
	public int getCurrentCatIndex() {
		return currentCatIndex;
	}


	public void setCurrentCatIndex(int currentCatIndex) {
		this.currentCatIndex = currentCatIndex;
	}


	public int getCurrentCatListIndex() {
		return currentCatListIndex;
	}


	public void setCurrentCatListIndex(int currentCatListIndex) {
		this.currentCatListIndex = currentCatListIndex;
	}


	public int getCurrentContentIndex() {
		return currentContentIndex;
	}


	public void setCurrentContentIndex(int currentContentIndex) {
		this.currentContentIndex = currentContentIndex;
	}


	@Override
	public GenTask next() {
		if(currentCatIndex<this.getCatNum()/2){
			GenTask task = new GenTask();
			task.setList(getList());
			task.setType(Publishable.CATEGORY_GEN);
			task.setMission(this);
			task.setAdmin(getAdmin());
			if(getList().size()==0){
				currentCatIndex = this.getCatNum()/2;
			}else{
				currentCatIndex+=getList().size();
			}
			return task;
		}else if(currentCatListIndex<this.getCatNum()/2){
			GenTask task = new GenTask();
			task.setList(getList());
			task.setType(Publishable.CATEGORY_LIST_GEN);
			task.setMission(this);
			task.setAdmin(getAdmin());
			if(getList().size()==0){
				currentCatListIndex = this.getCatNum()/2;
			}else{
				currentCatListIndex+=getList().size();
			}
			return task;
		}else if(currentContentIndex<this.getContentNum()){
			List<String> contentList = new ArrayList<String>();
			if(Publishable.ALL_PUBLISH.equals(this.getModelType())){
				contentList = generatable.findContentIdsByCatId(getList(),currentContentIndex, MAX);
			}else{
				contentList = generatable.findMoreContentIdsByCatId(getList(),currentContentIndex, MAX);
			}
			GenTask task = new GenTask();
//			task.setTaxPath(generatable.findTaxPath(getList()));
			task.setList(contentList);
			task.setType(Publishable.CONTENT_GEN);
			task.setMission(this);
			task.setAdmin(getAdmin());
			if(contentList.size()==0){
				currentContentIndex = this.getContentNum();
			}else{
				currentContentIndex+=contentList.size();
			}
			return task;
		}
		return null;
	}


	public boolean hasNext() {
		return currentCatIndex<this.getCatNum()/2
			||currentCatListIndex<this.getCatNum()/2
			||currentContentIndex<this.getContentNum();
	}


	public void remove() {
		
	}


}